package student.action;

import java.util.ArrayList;
import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;


import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.util.DB;
import com.util.MyLog;

import edu.student.models.Lookup;
/**
 * 
 * @author Sai Naga Sravani Peraka
 */
public class AdminSemCourses extends ActionSupport{
	private String semester;
	private String subject;
	private String semester_name="";
	private String subject_name="";
	private String commandButton = "";
	//private String WFNames="(";
	ArrayList<Lookup> courseList = new ArrayList<Lookup>();
	int semester_id=1;
	int subject_id=1;
	private String msg="";
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	 
	public String execute() throws Exception {
		
		
		 MyLog.log("Inside AdminSemCourses : Method : execute");

	        HttpServletRequest request=(HttpServletRequest)ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
	 
	        Enumeration paramNames= request.getParameterNames();
	 
	        while(paramNames.hasMoreElements())
	        {
	            String param=(String)paramNames.nextElement();
	            MyLog.log("\nParameter Name : "+param);
	            MyLog.log("Parameter Values : ");
	            if(param.equals("mySemester")){
	            	 String[] paramValues=request.getParameterValues(param);
		             
	 	            for(String paramVal:paramValues)
	 	            {
	 	            	Lookup semester=Lookup.findOne("semester", "where semester_name = '"+paramVal+"'");
	 	            	semester_id=semester.getId();
	 	            	semester_name=semester.getName();
	 	                MyLog.log(paramVal);
	 	            }
	            }
	            else if(param.equals("myCourse")){
	            	 String[] paramValues=request.getParameterValues(param);
		             
	 	            for(String paramVal:paramValues)
	 	            {
	 	            	Lookup subject=Lookup.findOne("subject", "where subject_name = '"+paramVal+"'");
	 	            	subject_id=subject.getId();
	 	            	subject_name=subject.getName();
	 	                MyLog.log(paramVal);
	 	            }
 	            	
	             }
	             
	        }
	
	        if (this.commandButton.startsWith("Add")) {
	        	
	        	int result=insert();
          		 if(result>0){
          			 msg="Added course "+subject_name +" successfully to semester "+semester_name+"!!";
          			 MyLog.log("Added course "+subject_name +" successfully to semester "+semester_name+"!!");
          		 }
          		 else{
          			//Lookup subject=Lookup.findOne("course", "where subject_id="+subject_id);
          			msg="Cannot Add, course "+subject_name +" already added to other semester. See View all records link for further information";
          			 MyLog.log("Cannot Add, course "+subject_name +" already added to other semester. See View all records link for further information");
          			 return "displayMessage";
          		 }
     	 	
          		 return "displayMessage";
            }
            else if (this.commandButton.startsWith("Remove")) {
            	int result=Lookup.findOnefordelete("course"," where semester_id="+semester_id+" and subject_id="+subject_id);
            	  if(result>0){
            		  	msg="Removed course "+subject_name +" successfully from semester "+semester_name +"!!";
          			 MyLog.log("Removed course "+subject_name +" successfully from semester "+semester_name +"!!");
          		 }
          		 else{
          			 
          			msg="Cannot Remove, Course "+subject_name +"not added to the semester "+semester_name +". Please try again!!";
          			MyLog.log("Cannot Remove, Course "+subject_name +" not added to the semester "+semester_name +". Please try again!!");
          			 return "displayMessage";
          		 }
            	return "displayMessage";
            }
            else if (this.commandButton.startsWith("View")) {
            	this.courseList = Lookup.findCourse("");
            	return "course_list";
            }
            else if (this.commandButton.startsWith("Back to Admin")) {
      			return "admin_home";
      		}
	
	    	return "initial";
	}
	public String getCommandButton() {
		return commandButton;
	}
	public void setCommandButton(String commandButton) {
		this.commandButton = commandButton;
	}
	public ArrayList<Lookup> getCourseList() {
		return courseList;
	}
	public void setCourseList(ArrayList<Lookup> courseList) {
		this.courseList = courseList;
	}
	public int insert() {
		String insertSQL = "insert into course(semester_id,subject_id) values" +
				"				("+semester_id +", "+subject_id+");";
		MyLog.log("insert query"+insertSQL);
		return DB.update(insertSQL);
	}
}
